<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            #result {
                width: 400px;
                height: 300px;
                border: solid 1px #666;
            }
        </style>
    </head>
    <body>
        <a href="#home">Home</a>
        <a href="#about">About</a>
        <hr />
        <div id="result"></div>

        <script>
            let result = document.querySelector('#result');
            //hashchange 事件   当网页 URL 的 hash(锚点) 值发生改变时, 会触发这个事件
            window.addEventListener("hashchange", function () {
                //获取切换后 url 的 hash 值, 页面的跳转, 刷新, 获得页面的完整 URL
                // console.log(window.location);
                let hash = window.location.hash;
                //判断
                switch (hash) {
                    case "#home":
                        result.innerHTML = '首页'
                        break;

                    case "#about":
                        result.innerHTML = '关于我们'
                        break;

                    default: 
                        result.innerHTML = '找不到网页';
                        break;
                }
            });
        </script>
    </body>
</html>
